<?php

namespace app\api\controller;

use app\common\model\Member;
use app\common\model\News;

use Exception;

class Base
{

    public function __construct()
    {
        try {
            //数据校验
            $count = 0;
            $params = array('token');
            foreach ($_POST as $key => $value) if (in_array($key, $params)) $count++;
            //暴力破解
            if ($count != sizeof($params)) output_error_param();
        } catch (Exception $e) {
            output_error($e->getMessage());
        }
    }

    //读取新闻列表
    public function news()
    {
        try {
            //数据校验
            $page = post_param('page');
            if (empty($page)) output_error_param();
            //读取数据
            $newsModel = new News();
            output_data($newsModel->getList($page));
        } catch (Exception $e) {
            output_error($e->getMessage());
        }
    }

    //用户注册接口(就是向数据表里面加入一条数据)
    public function register()
    {
        try {
            //参数校验
            $account = post_param('account');
            $password = post_param('password');
            if (empty($account) || empty($password)) output_error_param();
            //实例模型
            $memberModel = new Member();
            //验证用户是否存在
            $data = $memberModel->where('member_account', '=', $account)->find();
            if (!empty($data)) output_error('账号已存在');
            //所有的验证都处理完了，验证合法，注册用户
            $data = array();
            $data['member_account'] = $account;
            $data['member_password'] = md5_encrypt($account, $password);
            $data['member_nickname'] = '用户' . $account;
            $data['member_avatar'] = 'avatar.png';
            $data['member_state'] = COMMON_ENABLE;
            $data['add_time'] = time();
            if ($memberModel->insert($data)) output_success();
            output_error('注册失败');
        } catch (Exception $e) {
            output_error($e->getMessage());
        }
    }

    //用户登录（就是与数据库的记录进行对比）
    public function login()
    {
        try {
            //参数校验
            $account = post_param('account');
            $password = post_param('password');
            if (empty($account) || empty($password)) output_error_param();
            //实例模型
            $memberModel = new Member();
            //验证用户是否存在
            $data = $memberModel->where('member_account', '=', $account)->find();
            if (empty($data)) output_error('账号不存在');
            $condition = array();
            $condition[] = ['member_account', '=', $account];
            $condition[] = ['member_password', '=', md5_encrypt($account, $password)];
            $data = $memberModel->where($condition)->find();
            if (empty($data)) output_error('账号或密码不正确');
            //生成Token
            $token = md5_encrypt($account, $password . time());
            $memberModel->where('member_id', '=', $data['member_id'])->update(array('member_token' => $token));
            output_data($token);
        } catch (Exception $e) {
            output_error($e->getMessage());
        }
    }

}
